package com.axs.camera.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.axs.camera.R;
import com.axs.camera.bean.PhotoResultBean;
import com.bumptech.glide.Glide;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ResultAdapter extends RecyclerView.Adapter<ResultAdapter.ResultViewHolder> {
    private List<PhotoResultBean.ResultDTO> mList;
    private Context mContext;

    private OnItemClickListener onItemClickListener;

    public ResultAdapter(List<PhotoResultBean.ResultDTO> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @NotNull
    @Override
    public ResultViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new ResultViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ResultViewHolder holder, int position) {
        PhotoResultBean.ResultDTO bean = mList.get(position);
        if (bean != null) {
            holder.tvName.setText(bean.getName());
            holder.tvScore.setText(String.valueOf(bean.getScore()*100+"%"));
//            holder.tvDescription.setText(bean.getBaikeInfo().getDescription());

            Glide.with(mContext)
                    .load(bean.getBaikeInfo().getImageUrl())
                    .into(holder.tvPhoto);

            holder.root.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(bean);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    static class ResultViewHolder extends RecyclerView.ViewHolder {
        ImageView tvPhoto;
        TextView tvName;
        TextView tvScore;
        TextView tvDescription;
        ConstraintLayout root;

        public ResultViewHolder(@NonNull View itemView) {
            super(itemView);

            tvPhoto = itemView.findViewById(R.id.tv_photo);
            tvDescription = itemView.findViewById(R.id.tv_description);
            tvName = itemView.findViewById(R.id.tv_name);
            tvScore = itemView.findViewById(R.id.tv_score);
            root = itemView.findViewById(R.id.root);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(PhotoResultBean.ResultDTO bean);
    }
}
